package gateway.filter;



import blog.common.pojo.Result;
import blog.common.utils.JWTUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import gateway.properties.AuthWhiteName;
import io.jsonwebtoken.Claims;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.List;

@Slf4j
@Component
public class AuthFilter implements GlobalFilter, Ordered {
    @Autowired
    private AuthWhiteName authWhiteName;

    @Autowired
    private ObjectMapper objectMapper;




    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request =  exchange.getRequest();
        String path = request.getURI().getPath();
        if (match(path,authWhiteName.getUrl())) {
            return chain.filter(exchange);
        }

        //获取token
        String userToken = request.getHeaders().getFirst("user_token");
        if (userToken == null) {
            return unauthorizedResponse(exchange,"令牌为空！");
        }

        //验证token
        Claims claims = JWTUtils.parseJWT(userToken);
        if (claims == null) {
            log.info("user token is null");
            return unauthorizedResponse(exchange,"令牌异常！");
        }

        return chain.filter(exchange);

    }

    private boolean match(String path, List<String> url) {
        if (url == null || url.size() == 0) {
            return false;
        }
        return url.contains(path);
    }


    @SneakyThrows
    private Mono<Void> unauthorizedResponse(ServerWebExchange exchange, String message) {
        ServerHttpResponse response = exchange.getResponse();
        response.setStatusCode(HttpStatus.UNAUTHORIZED);
        response.getHeaders().add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
        Result result = Result.fail(message);
        DataBuffer dataBuffer = response.bufferFactory().wrap(objectMapper.writeValueAsBytes(result));
        return response.writeWith(Mono.just(dataBuffer));
    }


    @Override
    public int getOrder() {
        return 0;
    }
}
